package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.demo.pojo.Email;
import com.example.demo.pojo.Fasong;
import com.example.demo.pojo.Student;
import com.example.demo.pojo.User;
import com.example.demo.service.AddExcel;
import com.example.demo.service.MailServiceImpi;
import com.example.demo.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@RestController
@Slf4j
public class TestController {

    @Autowired
    private AddExcel addExcel;

    @Autowired
    private MailServiceImpi mailService;


    @RequestMapping("/test")
    public String test(){
        return "hello world";
    }


    //导入excel数据，数据库中有就更改，没有就插入。
    @GetMapping("/hello")
    public String hello(){
        try (FileInputStream inputStream = new FileInputStream("D:\\桌面\\期末作业\\student.xlsx")) {
            //easyexcel读取数据
            List<Student> users = ExcelUtil.readExcel(new BufferedInputStream(inputStream), Student.class);
            //mybatis读取数据
            List<Student> users1=addExcel.getAll();
            System.out.println(users1);
            if(users==null){
                return "文件内数据为空";
            }

            int sum=0;
            //for循环判断，log(2n)计算级不算大，可以用。循环判断excel中的数据是否存在于数据库，存在就更改，不存在就插入
            for(Student test:users){
                int j=0;
                //记录第几行出错
                sum++;
                if(test.getId()==null){
                    return "用户编号不能为空,错误行:"+String.valueOf(sum);
                }
                for(int i=0;i<users1.size();i++){
                    //判断旧值
                    if(users1.get(i).getId()==test.getId()){
                        if(test.getNum()>999){
                            return "年龄确定这么?错误行数:"+String.valueOf(sum);
                        }
                        //完全没有变化的旧值
                        if(test.getName()==users1.get(i).getName()){
                            if(test.getNum()==users1.get(i).getNum()){
                                if(test.getCity()==users1.get(i).getCity()){
                                    break;
                                }
                            }
                        }
                        //旧值有空值
                        if(test.getName()==null||test.getCity()==null||test.getNum()==null){
                            return "有空值,在"+String.valueOf(sum);
                        }
                        addExcel.updateStudent(test);
                        break;
                    }
                    //如果j==数据库中的数据数目就为新值
                    j++;
                }
                //判断是否为新值，是就插入
                if(j==users1.size()){
                    if(test.getNum()==null||test.getCity()==null||test.getName()==null){
                        return "空值异常"+String.valueOf(sum);
                    }
                    if(test.getName().length()>20){
                        return "会有这么长的名字吗？";
                    }
                    addExcel.insertStudent(test);
                }
            }
            System.out.println(users+"打印成功");
            return "hello";
        } catch (IOException e) {
            e.printStackTrace();
            return "错误";
        }
    }


    //测试发送邮件
    @RequestMapping(value = "/sendMail")
    public String sendEmail() {
        Email email_email = new Email();
        List<String> addressList = new ArrayList<String>();
        addressList.add("2799401674@qq.com");
        //目标人
        email_email.setToAddress(addressList);
        email_email.setSubject("java碎碎念-主题测试");// 主题
        //信息主体
        email_email.setContent("你好！<br><br> 测试邮件发送成功！");
        // 发送邮件方法
        mailService.sendMail(email_email);

        return "ok";
    }



    //测试发送邮件
    @RequestMapping(value = "/song")
    public String song() {
        Email email = new Email();
        try (FileInputStream inputStream = new FileInputStream("D:\\桌面\\期末作业\\发送邮件.xlsx")) {
            //easyexcel读取数据
            List<Fasong> song = ExcelUtil.readExcel(new BufferedInputStream(inputStream), Fasong.class);
            if(song==null){
                return "文件内容为空";
            }
            for(Fasong test:song){
                List<String> addressList = new ArrayList<String>();
                addressList.add(test.getAddress());
                email.setToAddress(addressList);
                email.setSubject("excel发送邮件测试");
                email.setContent("你好！<br><br> 测试邮件发送成功！<br><br>姓名:"+test.getName());
                mailService.sendMail(email);
            }
            return "发送成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "错误";
        }
    }





}
